<?php

namespace App\Backend\Http\Controllers;

use App\Cat;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class CatController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index($id)
    {
        $data['cats'] = Cat::where('type',$id)->orderBy('id','desc')->get();
        $data['cats'] = collect($data['cats'])->groupBy('level');
        $data['type'] = $id;
        return view('backend::cat.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($id,$type)
    {
        $data['cat'] = Cat::find($id);
        $data['cats'] = Cat::where(['level'=>2,'type'=>$type])->get();
        $data['type'] = $type;
        return view('backend::cat.create',$data);
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        $data['level'] = Cat::where('id',$data['father_id'])->value('level')+1;
        if($data['id']){
            Cat::where('id',$data['id'])->update($data);
        }else{
            Cat::create($data);
        }
        return redirect('/backend/catlist/'.$data['type']);
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy()
    {
    }
}
